package com.crm.vo;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class GetPinyin {
	
	/**
	 * 将汉字转成拼音
	 * @param src
	 * @return
	 *//*
	public static String getPinYin(String src) {
		if (src == null || src.equals("")) return null;
		char[] t1 = null;
		t1 = src.toCharArray();
		String[] t2 = new String[t1.length];
		HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
		t3.setCaseType(HanyuPinyinCaseType.UPPERCASE);
		t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		t3.setVCharType(HanyuPinyinVCharType.WITH_V);
		String t4 = "";
		int t0 = t1.length;
		try {
			for (int i = 0; i < t0; i++) {
				//判断是否为汉字字符
				if(java.lang.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
					t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
					t4 += t2[0];
				}
			}
			return t4;
		} catch (BadHanyuPinyinOutputFormatCombination e) {
			e.printStackTrace();
		}
		return t4;
	}
	
	*//**
	 * 获得字符串每次汉字的首字母
	 * @param str
	 * @return
	 *//*
	public static String getPinYinHeadChar(String str) {
		if (str == null || str.equals("")) return null;
		String convert = "";
		for (int j = 0; j < str.length(); j++) {
			char word = str.charAt(j);
			String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
			if (pinyinArray != null) {
				convert += pinyinArray[0].charAt(0);
			} 		
		}
		
		return convert.toUpperCase();
	}*/
	
	/**
	 * 获得一个字符串第一个汉字的首字母
	 * @param str
	 * @return
	 */
	public static String getStringFirstChar(String str) {
		if (str == null || str.equals("")) return null;
		String convert = "";
		//获取首个字符
		char word = str.charAt(0);
		//如果首个字符是英文 则该字母就是首字母
		if(word>=0x0000 && word<=0x00FF) {
			convert += word;
		}
		//如果首个字符是中文 则需要转成英文 获取首字母
		if (word >= 0x0391 && word <= 0xFFE5) {
			String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
			if (pinyinArray != null) {
				convert += pinyinArray[0].charAt(0);
			} 	
		}
		return convert.toUpperCase();
	}
}
